package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@SuppressWarnings("all")
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void addCourse(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "id","description","createTime","updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public PageResult<Course> page( String name,Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> list = courseMapper.page(name, subject, target);
        Page<Course> rows = (Page<Course>)list;
        return PageResult.<Course>builder().total(rows.getTotal()).rows(rows.getResult()).build();
    }

    @Override
    public Course selectId(Integer id) {
        Course course=courseMapper.selectId(id);
        return course;

    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);
    }

    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleteCourse(id);
    }

    @Override
    public List<Course> list() {
        return courseMapper.selectAll();
    }

    @Override
    public List<Course> selectSubject(Integer subject) {
        return courseMapper.selectSubject(subject);
    }
}
